package com.cnkvha.trackcontestmgmt.contest;

import org.webbitserver.helpers.Base64;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class ContestTrackInfo {

    public String name;
    public String participant;

    public Map<String, Long> map_time_spent;

    public int realtime_order = -1;
    public long avg_time_spent = 0;

    // runtime only, for display, DOES NOT AFFECT CALCULATIONS!!!
    public int count_timer = 0;
    public int received_timer_stop = 0;
    public long total_time_spent_for_avg_calc = 0;

    public boolean confirmed = false;
    public long confirmed_time_spent = -1L;
    public int confirmed_order = -1;


    // for deserialization
    public ContestTrackInfo() {
        this.map_time_spent = new HashMap<>();
    }

    public ContestTrackInfo(String name, String participant) {
        this.name = name;
        this.participant = participant;
        this.map_time_spent = new HashMap<>();
    }

    public ContestTrackInfo(String name, String participant, Map<String, Long> map_time_spent, boolean confirmed, long confirmed_time_spent, int confirmed_order) {
        this.name = name;
        this.participant = participant;
        this.map_time_spent = map_time_spent;
        this.confirmed = confirmed;
        this.confirmed_time_spent = confirmed_time_spent;
        this.confirmed_order = confirmed_order;
    }

    public String serializeIntoTCMData() {
        String d = String.format("%s;%s;%s;%s;%s", name, participant, confirmed ? "confirmed" : "no", confirmed_time_spent, confirmed_order);
        return Base64.encode(d.getBytes(StandardCharsets.UTF_8));
    }

    public void resetRuntimeData() {
        count_timer = 0;
        received_timer_stop = 0;
        total_time_spent_for_avg_calc = 0;
        realtime_order = -1;
    }

}
